<template>
  <div>
    <div class="table-tool">
      <el-button class="el-button--blue" type="primary" icon="el-icon-upload" @click="exportStudent">导出</el-button>
      <el-button class="el-button--blue" type="primary" icon="el-icon-plus" @click="addDialog.visible=true">添加</el-button>
      <el-button class="el-button--blue" type="primary" icon="el-icon-delete" @click="deleteStudent">删除</el-button>
    </div>
    <el-table ref="table_container" header-align="center" :data="data" stripe :height="tableHeight" v-loading="loading" @selection-change="handleSelectionChange" header-cell-class-name="table-header-cell" cell-class-name="table-cell">
      <el-table-column type="selection" fixed/>
      <el-table-column prop="schoolRollId" label="学籍号" width="200px"/>
      <el-table-column prop="name" label="姓名" width="150px"/>
      <el-table-column prop="schoolName" label="学校名称" width="200px"/>
      <el-table-column prop="sex" label="性别" width="150px"/>
      <el-table-column prop="grade" label="年级" width="150px"/>
      <el-table-column prop="class" label="班级" width="150px"/>
      <el-table-column prop="age" label="年龄" width="150px"/>
      <el-table-column prop="score" label="最近体测成绩" width="150px"/>
      <el-table-column prop="birthday" label="生日" width="150px"/>
      <el-table-column prop="identityId" label="身份证号" width="200px"/>
      <el-table-column prop="nation" label="民族" width="150px"/>
      <el-table-column prop="address" label="家庭住址" width="150px"/>
      <el-table-column prop="phone" label="家长电话" width="150px"/>
      <el-table-column prop="studentId" label="学号" width="200px"/>
      <el-table-column align="center" label="操作" width="300px" fixed="right">
        <template  slot-scope="scope">
          <el-button class="text-button" type="text" icon="el-icon-refresh blue">重置密码</el-button>
          <el-button class="text-button" type="text" icon="el-icon-edit blue" @click="changeInfoDialogShow(scope.$index)">修改信息</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="changeDialog.title" :visible.sync="changeDialog.visible" width="1200px">
      <student-change v-model="changeDialog"/>
    </el-dialog>
    <el-dialog :title="addDialog.title" :visible.sync="addDialog.visible" width="1200px">
      <student-add v-model="addDialog"/>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { GetStudentTable, GetStudentExport } from '../../../api/table'
import studentChange from '../dialog/student-change'
import { nation, grade, SubmitStudentDelete, CheckHasUserName, SubmitPasswordChange } from '../../../api/form'
import { ParseChinese } from '../../../utils/date'
import studentAdd from '../dialog/student-add'
export default {
  name: 'student',
  components: {
    studentChange,
    studentAdd
  },
  data () {
    return {
      tableHeight: '0px',
      pagination: {
        pageSize: 10,
        currentPage: 1
      },
      data: [],
      loading: false,
      multipleSelection: [],
      total: 0,
      changeDialog: {
        title: '',
        visible: false,
        data: {
          id: '',
          name: '',
          schoolRollId: '',
          sex: '',
          nation: '',
          birthday: '',
          identityId: '',
          gradeYear: '',
          grade: '',
          class: '',
          studentId: '',
          phone: '',
          address: ''
        }
      },
      addDialog: {
        title: '',
        visible: false
      },
      changePasswordDialog: {
        visible: false,
        password: '',
        id: ''
      }
    }
  },
  mounted () {
    this.$nextTick(this.reInitTableHeight)
  },
  methods: {
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.pagination.pageSize = val
    },
    handleCurrentChange (val) {
      this.pagination.currentPage = val
    },
    flashTable () {
      this.loading = true
      GetStudentTable(this.pagination.pageSize, this.pagination.currentPage).then((res) => {
        this.total = res.data.total
        console.log(res.data.total)
        this.data = res.data.data
        this.loading = false
      })
    },
    changeInfoDialogShow (index) {
      this.changeDialog.title = this.data[index].schoolName + ' ' + this.data[index].name
      this.changeDialog.data.id = this.data[index].id
      this.changeDialog.data.schoolRollId = this.data[index].schoolRollId
      this.changeDialog.data.name = this.data[index].name
      this.changeDialog.data.sex = this.data[index].sex
      this.changeDialog.data.nation = nation[this.data[index].nation]
      this.changeDialog.data.birthday = ParseChinese(this.data[index].birthday)
      this.changeDialog.data.identityId = this.data[index].identityId
      this.changeDialog.data.gradeYear = Number(this.data[index].grade.substring(0, 4))
      this.changeDialog.data.grade = grade[this.data[index].grade.substring(6, this.data[index].grade.length)].toString()
      this.changeDialog.data.class = this.data[index].class
      this.changeDialog.data.studentId = this.data[index].studentId
      this.changeDialog.data.phone = this.data[index].phone
      this.changeDialog.data.address = this.data[index].address
      this.changeDialog.visible = true
    },
    deleteStudent () {
      SubmitStudentDelete(this.multipleSelection).then(res => {
        this.multipleSelection = []
        this.$store.dispatch('SetFlash')
      })
    },
    reInitTableHeight () {
      if (document.documentElement.clientHeight - 70 - this.$refs['table_container'].$el.offsetTop < 0) {
        setTimeout(this.reInitTableHeight, 10)
      } else if (document.documentElement.clientHeight - 70 - this.$refs['table_container'].$el.offsetTop <= 500) {
        this.tableHeight = '500px'
      } else {
        this.tableHeight = document.documentElement.clientHeight - 70 - this.$refs['table_container'].$el.offsetTop + 'px'
      }
    },
    exportStudent () {
      setTimeout(this.reInitTableHeight, 100)
    }
  },
  computed: {
    ...mapGetters([
      'height',
      'provinceId',
      'cityId',
      'areaId',
      'joinYear',
      'schoolName',
      'schoolId',
      'schoolRollId',
      'flash'
    ])
  },
  watch: {
    height: function (val) {
      this.$nextTick(this.$nextTick(this.reInitTableHeight))
    },
    'pagination.currentPage': {
      handler () {
        this.flashTable()
      },
      immediate: true,
      deep: true
    },
    'pagination.pageSize': {
      handler () {
        this.flashTable()
      },
      immediate: true,
      deep: true
    },
    provinceId: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    cityId: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    joinYear: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    areaId: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    schoolName: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    schoolId: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    schoolRollId: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    flash: function () {
      this.flashTable()
    }
  }
}
</script>

<style scoped>
  @import "../../../styles/common.css";
  .table-tool {
    width: 100%;
    padding: 15px 10px;
  }
</style>
